﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net.Mail;

using planetExpressLib;

namespace planet_express
{
    public partial class login : System.Web.UI.Page
    {
        clienteEN usuario;

        protected void Page_Load(object sender, EventArgs e)
        {
            MultiView.SetActiveView(ViewLogin);
            usuario = null;
        }

        protected void aceptar_user(object sender, EventArgs e) {

            clientesCAD cad = new clientesCAD();

            if (user_text.Text == "" || pass_text.Text == "")
            {
                Info.SetActiveView(faltan_datos); //error de introduccion de datos
            }
            else
            {
                usuario = cad.readUsuario(user_text.Text);

                if (usuario == null)
                {
                    Info.SetActiveView(error_usuario);
                }
                else if (usuario.Pass != pass_text.Text)
                {
                    Info.SetActiveView(error_contrasena);
                }
                else
                {

                    //guardo variables de sesion
                    Session["login"] = usuario.Usuario;
                    Session["nombre"] = usuario.Nombre;
                    Response.Redirect("Default.aspx"); //redirecciono a la pagina principal por ahora
                }
            }
          
        }

        protected void UsuarioRecovery_SendingMail(object sender, MailMessageEventArgs e)
        {
            MailMessage msg = new MailMessage();
                

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            try
            {

                foreach (clienteEN c in (new clientesCAD().readAll()))
                {
                    if (c.Mail == UsuarioRecovery.UserName)
                    {
                        usuario = new clienteEN();
                        usuario = c;
                    }
                }

                if (usuario != null)
                {
                    msg.Subject = "Recupera tus datos";
                    msg.Body = "Tu usuario es " + usuario.Usuario;
                    msg.From = new MailAddress("planetexpress2013@gmail.com");
                    msg.To.Add(UsuarioRecovery.UserName);
                    msg.IsBodyHtml = true;
                    client.Host = "smtp.gmail.com";
                    System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("planetexpress2013@gmail.com", "todoslocos");
                    client.Port = int.Parse("587");
                    client.EnableSsl = true;
                    client.UseDefaultCredentials = false;
                    client.Credentials = basicauthenticationinfo;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Send(msg);
                }
                else
                {
                    Info.SetActiveView(error_usuario);
                }
            }
            catch (Exception ex)
            {
                Info.SetActiveView(error_usuario);
            }
        }

        protected void PasswordRecovery_SendingMail(object sender, MailMessageEventArgs e)
        {
            MailMessage msg = new MailMessage();
                

            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
            try
            {

                foreach (clienteEN c in (new clientesCAD().readAll()))
                {
                    if (c.Usuario == UsuarioRecovery.UserName)
                    {
                        usuario = new clienteEN();
                        usuario = c;
                    }
                }

                if (usuario != null)
                {
                    msg.Subject = "Recupera tus datos";
                    msg.Body = "Tu contraseña es " + usuario.Pass;
                    msg.From = new MailAddress("planetexpress2013@gmail.com");
                    msg.To.Add(UsuarioRecovery.UserName);
                    msg.IsBodyHtml = true;
                    client.Host = "smtp.gmail.com";
                    System.Net.NetworkCredential basicauthenticationinfo = new System.Net.NetworkCredential("planetexpress2013@gmail.com", "todoslocos");
                    client.Port = int.Parse("587");
                    client.EnableSsl = true;
                    client.UseDefaultCredentials = false;
                    client.Credentials = basicauthenticationinfo;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;
                    client.Send(msg);
                }
                else
                {
                    Info.SetActiveView(error_contrasena);
                }
            }
            catch (Exception ex)
            {
                Info.SetActiveView(error_contrasena);
            }
        }

        protected void ButtonUsuario_Click(object sender, EventArgs e)
        {
            Info.Visible = false;
            MultiView.SetActiveView(ViewUsuario);
        }

        protected void ButtonPassword_Click(object sender, EventArgs e)
        {
            Info.Visible = false;
            MultiView.SetActiveView(ViewPassword);
        }
    }
}